// FaceDialog.cpp : implementation file
//

#include "stdafx.h"
//#include "chat.h"
#include "FaceDialog.h"
#include "MemDC.h"

//#ifdef _DEBUG
//#define new DEBUG_NEW
//#undef THIS_FILE
//static char THIS_FILE[] = __FILE__;
//#endif

#define WIDTH  50
#define HEIGHT 50  
#define COLUMN 10
#define ROW    2
/////////////////////////////////////////////////////////////////////////////
// CFaceDialog dialog


CFaceDialog::CFaceDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CFaceDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFaceDialog)
	//}}AFX_DATA_INIT
	this->select = -1;
	this->OnPicture = NULL;
	this->pContext = NULL;
}


void CFaceDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFaceDialog)
	DDX_Control(pDX, IDC_STATIC_FACE_GIF, m_gif);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFaceDialog, CDialog)
	//{{AFX_MSG_MAP(CFaceDialog)
	ON_WM_ACTIVATE()
	ON_WM_PAINT()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFaceDialog message handlers
BOOL CFaceDialog::Create( CWnd * pWnd )
{
	return CDialog::Create( IDD , pWnd );
}

BOOL CFaceDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();	
	this->MoveWindow( 0 , 0 , COLUMN * WIDTH + 1 , ROW * HEIGHT + 1 );

	this->LoadPicture();

	return TRUE;
}

void CFaceDialog::LoadPicture( void )
{
	CString dir;
	::GetModuleFileName( NULL , dir.GetBufferSetLength( MAX_PATH ) , MAX_PATH );
	dir.ReleaseBuffer();
	dir = dir.Left( dir.ReverseFind( '\\' ) + 1 ) + "faces\\*.*";
	//AfxMessageBox(dir);

	CFileFind find;
	BOOL found = find.FindFile( dir );
	
	int i = 0;	
	CPicture picture;
	
	while( found )
	{
		found = find.FindNextFile();
		//AfxMessageBox(find.GetFilePath());
		if( ! find.IsDots() && picture.Load( find.GetFilePath() ) )
		{
			this->picture_map[ i++ ] = find.GetFilePath();
		}
		//if (i>6) break;
	}
		//AfxMessageBox("1");
	CClientDC dc( this );
	CDC memDC;
	memDC.CreateCompatibleDC( &dc );
	
	CRect rc;
	this->GetClientRect( &rc );

	if( ! this->bitmap.GetSafeHandle() )
	{
		this->bitmap.CreateCompatibleBitmap( &dc , rc.Width() , rc.Height() );
		memDC.SelectObject( this->bitmap );
	}
	CPen pen( PS_SOLID , 1 , RGB( 128 , 128 , 128 ) );
	memDC.SelectObject( &pen );

	PICTURE_MAP::iterator itr;
	i = 0;
	for( int h = 0; h < rc.Height() - HEIGHT; h += HEIGHT )
		for( int w = 0; w < rc.Width() - WIDTH; w += WIDTH )
		{
			memDC.Rectangle( w , h , w + WIDTH + 1 , h + HEIGHT + 1 );
			if( ( itr = this->picture_map.find( i ) ) != this->picture_map.end() )
			{ 
				i ++;
				picture.Load( itr->second );
				picture.Render( &memDC , CRect( w + 2 , h + 2 , w + WIDTH - 2 , h + HEIGHT - 2 ) );
			}
		}
}

void CFaceDialog::Select( const CPoint & point )
{
	int x = point.x / WIDTH;
	int y = point.y / HEIGHT;

	this->select = x + y * COLUMN;

	if( this->select >= this->picture_map.size() ) return;

	CRect rc;
	rc.left =  x * WIDTH;
	rc.top	=  y * HEIGHT;
	rc.right = rc.left + WIDTH + 1;
	rc.bottom = rc.top + HEIGHT + 1;

	CClientDC dc( this );
	dc.SelectStockObject( NULL_BRUSH );
	
	static CRect rs;
	if( ! rs.IsRectEmpty() )
	{
		CPen pen( PS_SOLID , 1 , RGB( 128 , 128 , 128 ) );
		dc.SelectObject( &pen );
		dc.Rectangle( &rs );
	}
	rs = rc;
	CPen pen( PS_SOLID , 1 , RGB( 255 , 0 , 0 ) );
	dc.SelectObject( &pen );
	dc.Rectangle( &rs );

	CString gif = this->GetSelectPicture();
	if( ! gif.IsEmpty() )
	{
		this->m_gif.UnLoad();
		this->m_gif.Load( gif );
		this->m_gif.Draw();
	}
}

CString CFaceDialog::GetSelectPicture( void )
{
	PICTURE_MAP::iterator itr = this->picture_map.find( this->select );
	if( itr != this->picture_map.end() )
		return itr->second;
	return "";

}

void CFaceDialog::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
{
	CDialog::OnActivate(nState, pWndOther, bMinimized);
	
	if( nState == WA_INACTIVE )
	{
		this->m_gif.UnLoad();
		this->ShowWindow( SW_HIDE );
	}
}

void CFaceDialog::OnPaint() 
{
	CPaintDC dc( this );
	
	if( this->bitmap.GetSafeHandle() )
	{
		CRect rc;
		this->GetClientRect( &rc );

		CBrush b;
		b.CreatePatternBrush( &this->bitmap );
		CMemDC memDC( &dc );
		memDC.FillRect( &rc , &b );
	}
}

void CFaceDialog::OnMouseMove(UINT nFlags, CPoint point) 
{	
	CDialog::OnMouseMove(nFlags, point);

	this->Select( point );
}

void CFaceDialog::OnLButtonDown(UINT nFlags, CPoint point) 
{	
	CDialog::OnLButtonDown(nFlags, point);
	
	CString s = this->GetSelectPicture();
	if( ! s.IsEmpty() && this->OnPicture )

		this->OnPicture( this->pContext , s );
}
